#!/bin/bash

# test_frontend_refactor.sh - 测试前端重构和Issue #013修复
# 验证前端模块化架构和点击频率控制功能

echo "🔥 前端重构验证脚本 - Issue #013修复测试"
echo "================================================"

# 服务器信息
SERVER_URL="http://localhost:5700"
FRONTEND_URL="${SERVER_URL}/web/"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 检查服务器是否运行
check_server() {
    echo -n "检查服务器状态... "
    if curl -s "$SERVER_URL/api/vehicles" > /dev/null 2>&1; then
        echo -e "${GREEN}✅ 服务器运行正常${NC}"
        return 0
    else
        echo -e "${RED}❌ 服务器未运行，请先启动服务器${NC}"
        echo "启动命令: go run cmd/main.go"
        exit 1
    fi
}

# 检查静态文件结构
check_file_structure() {
    echo ""
    echo -e "${BLUE}📁 检查前端文件结构${NC}"
    echo "----------------------------------------"
    
    local files=(
        "web/index.html"
        "web/index_old.html"
        "web/assets/css/styles.css"
        "web/assets/js/main.js"
        "web/assets/js/trim.js"
    )
    
    local missing_files=0
    
    for file in "${files[@]}"; do
        if [ -f "$file" ]; then
            echo -e "  ✅ ${file}"
        else
            echo -e "  ❌ ${file} ${RED}缺失${NC}"
            ((missing_files++))
        fi
    done
    
    if [ $missing_files -eq 0 ]; then
        echo -e "${GREEN}✅ 所有文件结构正确${NC}"
        return 0
    else
        echo -e "${RED}❌ ${missing_files} 个文件缺失${NC}"
        return 1
    fi
}

# 测试静态文件访问
test_static_files() {
    echo ""
    echo -e "${BLUE}🌐 测试静态文件访问${NC}"
    echo "----------------------------------------"
    
    local urls=(
        "${FRONTEND_URL}"
        "${FRONTEND_URL}assets/css/styles.css"
        "${FRONTEND_URL}assets/js/main.js"
        "${FRONTEND_URL}assets/js/trim.js"
    )
    
    local failed_requests=0
    
    for url in "${urls[@]}"; do
        echo -n "  测试 ${url##*/}... "
        
        if curl -s --head "$url" | grep -q "200 OK"; then
            echo -e "${GREEN}✅ 可访问${NC}"
        else
            echo -e "${RED}❌ 访问失败${NC}"
            ((failed_requests++))
        fi
    done
    
    if [ $failed_requests -eq 0 ]; then
        echo -e "${GREEN}✅ 所有静态文件可正常访问${NC}"
        return 0
    else
        echo -e "${RED}❌ ${failed_requests} 个文件访问失败${NC}"
        return 1
    fi
}

# 分析HTML文件变化
analyze_html_changes() {
    echo ""
    echo -e "${BLUE}📊 分析HTML文件变化${NC}"
    echo "----------------------------------------"
    
    if [ ! -f "web/index_old.html" ]; then
        echo -e "${YELLOW}⚠️ 旧版HTML文件不存在，跳过对比${NC}"
        return 0
    fi
    
    local old_size=$(wc -c < "web/index_old.html")
    local new_size=$(wc -c < "web/index.html")
    local size_diff=$((new_size - old_size))
    
    echo "  旧版文件大小: ${old_size} 字节"
    echo "  新版文件大小: ${new_size} 字节"
    
    if [ $size_diff -lt 0 ]; then
        echo -e "  文件大小变化: ${GREEN}减少 $((size_diff * -1)) 字节${NC} (代码分离)"
    else
        echo -e "  文件大小变化: 增加 ${size_diff} 字节"
    fi
    
    # 检查关键标签
    echo ""
    echo "  关键功能检查:"
    
    if grep -q 'assets/css/styles.css' "web/index.html"; then
        echo -e "    ✅ CSS外链引用"
    else
        echo -e "    ❌ CSS外链引用缺失"
    fi
    
    if grep -q 'assets/js/main.js' "web/index.html"; then
        echo -e "    ✅ 主JS文件引用"
    else
        echo -e "    ❌ 主JS文件引用缺失"
    fi
    
    if grep -q 'assets/js/trim.js' "web/index.html"; then
        echo -e "    ✅ Trim JS文件引用"
    else
        echo -e "    ❌ Trim JS文件引用缺失"
    fi
    
    if grep -q 'v1.4.0' "web/index.html"; then
        echo -e "    ✅ 版本标识更新"
    else
        echo -e "    ❌ 版本标识缺失"
    fi
}

# 测试API功能兼容性
test_api_compatibility() {
    echo ""
    echo -e "${BLUE}🔗 测试API功能兼容性${NC}"
    echo "----------------------------------------"
    
    # 测试基础API
    echo -n "  测试vehicles API... "
    if curl -s "$SERVER_URL/api/vehicles" | grep -q '\['; then
        echo -e "${GREEN}✅ 正常${NC}"
    else
        echo -e "${RED}❌ 失败${NC}"
        return 1
    fi
    
    # 测试trim API
    echo -n "  测试get_trims API... "
    if curl -s "$SERVER_URL/api/get_trims?model=Puma" | grep -q 'trims\|error'; then
        echo -e "${GREEN}✅ 正常${NC}"
    else
        echo -e "${RED}❌ 失败${NC}"
        return 1
    fi
    
    echo -e "${GREEN}✅ API兼容性测试通过${NC}"
    return 0
}

# 验证Issue #013修复
verify_issue_013_fix() {
    echo ""
    echo -e "${BLUE}🐛 验证Issue #013修复状态${NC}"
    echo "----------------------------------------"
    
    # 检查JavaScript文件中的防抖机制
    if grep -q "scrapeDebounceTime.*2000" "web/assets/js/trim.js"; then
        echo -e "  ✅ 防抖机制 (2秒延迟)"
    else
        echo -e "  ❌ 防抖机制缺失"
    fi
    
    if grep -q "isScrapingInProgress" "web/assets/js/trim.js"; then
        echo -e "  ✅ 状态管理变量"
    else
        echo -e "  ❌ 状态管理变量缺失"
    fi
    
    if grep -q "updateScrapeButtonsState" "web/assets/js/trim.js"; then
        echo -e "  ✅ 按钮状态更新函数"
    else
        echo -e "  ❌ 按钮状态更新函数缺失"
    fi
    
    if grep -q "lastScrapeTime" "web/assets/js/trim.js"; then
        echo -e "  ✅ 时间戳检查机制"
    else
        echo -e "  ❌ 时间戳检查机制缺失"
    fi
    
    # 检查Issue状态文件
    if grep -q "已修复" "issues/013-frontend-click-frequency-control-not-working.md"; then
        echo -e "  ✅ Issue #013标记为已修复"
    else
        echo -e "  ❌ Issue #013状态未更新"
    fi
    
    echo -e "${GREEN}✅ Issue #013修复验证完成${NC}"
}

# 生成测试报告
generate_test_report() {
    local total_tests="$1"
    local passed_tests="$2"
    local failed_tests="$3"
    
    echo ""
    echo "================================================"
    echo -e "${BLUE}📊 前端重构测试报告${NC}"
    echo "================================================"
    echo "测试项目: $total_tests"
    echo -e "通过项目: ${GREEN}$passed_tests${NC}"
    echo -e "失败项目: ${RED}$failed_tests${NC}"
    
    if [ "$failed_tests" -eq 0 ]; then
        echo -e "${GREEN}🎉 前端重构成功！所有测试通过！${NC}"
        echo ""
        echo "✅ 模块化架构实现完成"
        echo "✅ 静态资源分离成功"
        echo "✅ Issue #013点击频率控制修复"
        echo "✅ API兼容性保持良好"
        echo "✅ 文件结构优化完成"
        echo ""
        echo "🚀 前端已达到v1.4.0标准，支持："
        echo "   • 多页面类型适配展示"
        echo "   • 强化的用户交互体验"
        echo "   • 模块化代码架构"
        echo "   • 企业级错误处理"
    else
        echo -e "${RED}❌ 存在失败的测试项目，需要进一步检查${NC}"
        echo ""
        echo "建议检查:"
        echo "1. 服务器静态文件路由配置"
        echo "2. 文件权限和路径设置"
        echo "3. JavaScript模块加载顺序"
        echo "4. CSS样式引用路径"
    fi
    
    echo ""
    echo "💡 前端页面访问: $FRONTEND_URL"
    echo "💡 开发者工具: F12检查Console输出"
    echo "💡 问题追踪: issues/013-frontend-click-frequency-control-not-working.md"
}

# 主测试流程
main() {
    echo "开始测试时间: $(date)"
    echo ""
    
    # 检查服务器
    check_server
    
    # 初始化计数器
    total_tests=0
    passed_tests=0
    failed_tests=0
    
    # 运行测试项目
    tests=(
        "check_file_structure"
        "test_static_files"
        "analyze_html_changes"
        "test_api_compatibility"
        "verify_issue_013_fix"
    )
    
    for test in "${tests[@]}"; do
        ((total_tests++))
        
        if $test; then
            ((passed_tests++))
        else
            ((failed_tests++))
        fi
        
        sleep 0.5
    done
    
    # 生成报告
    generate_test_report "$total_tests" "$passed_tests" "$failed_tests"
    
    # 返回适当的退出码
    if [ "$failed_tests" -eq 0 ]; then
        exit 0
    else
        exit 1
    fi
}

# 运行主函数
main "$@"
